package lee.vioson.ebook.viewmodels

import androidx.lifecycle.viewModelScope
import kotlinx.coroutines.flow.SharingStarted
import kotlinx.coroutines.flow.StateFlow
import kotlinx.coroutines.flow.catch
import kotlinx.coroutines.flow.stateIn
import lee.vioson.composecommon.base.mvi.BaseViewModel
import lee.vioson.composecommon.base.mvi.Reducer
import lee.vioson.composecommon.base.mvi.UiEvent
import lee.vioson.composecommon.base.mvi.UiState
import lee.vioson.ebook.data.AppContainer
import lee.vioson.remotedata.manager.LoginType
import lee.vioson.remotedata.manager.UserManager

/**
 *Author:viosonlee
 *Date:2023/6/10
 *DESCRIPTION:
 */
class MyViewModel(appContainer: AppContainer) :
    AppViewModel<MyViewModel.State, MyViewModel.Event>(appContainer.appEvent) {

    private val reducer = SubReducer(State())

    override val state: StateFlow<State>
        get() = reducer.state

    override fun sendEvent(event: Event) {
        reducer.sendEvent(event)
    }

    val loginResult = UserManager.INSTANCE.getLoginResult(appContainer.context)
        .catch { handleException(it) }
        .stateIn(viewModelScope, SharingStarted.WhileSubscribed(5000), null)

    private class SubReducer(initialState: State) :
        Reducer<State, Event>(initialState) {
        override fun reduce(oldState: State, event: Event) {

        }
    }


    sealed class Event : UiEvent {

    }

    data class State(val isVip: Boolean = false, val loginType: LoginType = LoginType.ANONYMOUS) :
        UiState
}

